package com.selenium.test;
import static com.selenium.test.DriverScript.APP_LOGS;
import static com.selenium.test.DriverScript.CONFIG;
import static com.selenium.test.DriverScript.ELEMENTS;

import javax.lang.model.element.Element;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Keywords {

	public static WebDriver driver;
	
	
	public String openBrowser(String object, String data){
		
		APP_LOGS.debug("Opening browser");
		
		if(CONFIG.getProperty(data).equals("Firefox")){
			driver = new FirefoxDriver();
		}
		else if (CONFIG.getProperty(data).equals("IE")){
			System.setProperty( "webdriver.ie.driver" , System.getProperty("user.dir")+"//src//com//staffingboss//driverFiles//IEDriverServer.exe" );
			driver = new InternetExplorerDriver();
		}
		else if (CONFIG.getProperty(data).equals("Chrome")){
			System.setProperty( "webdriver.ie.driver" , System.getProperty("user.dir")+"//src//com//staffingboss//driverFiles//IEDriverServer.exe" );
			driver = new ChromeDriver();	
		}
		
		
		return Constants.PASS;
	}
		
	public String navigate(String object, String data){		
			
			APP_LOGS.debug("Navigating to URL");
			
			try{
				
			driver.navigate().to(CONFIG.getProperty(data));
			
			}
			catch(Exception e){
				return Constants.FAIL + " Not able to navigate the url";
			}
			
			return Constants.PASS;
		
	}
		
		
	public String click(String object, String data){
	        APP_LOGS.debug("Clicking on link ");
	        
	        try{
	        	
	        driver.findElement(By.xpath(ELEMENTS.getProperty(object))).click();
	        
	        }
	        catch(Exception e){
	        	return Constants.FAIL + "-- Unable to click on " + object;
	        }
	        return Constants.PASS;
		
	}
	
	
	public String enterKey(String object, String data){
		
		APP_LOGS.debug("-- Write input --" + object + "with :" + data);
		try{
			
			WebElement enterField = driver.findElement(By.xpath(ELEMENTS.getProperty(object)));
			enterField.sendKeys(data);
			return Constants.PASS;
			
		}catch(Exception e){
			return Constants.FAIL + "Unable to write the input" + data + "to " + object ;
		}
		
	}	
	
	
	public String verifyText(String object, String data){
	       
			APP_LOGS.debug("-- VERIFY LINK TEXT --");
			APP_LOGS.debug(object);
			APP_LOGS.debug(data);
			try{
				
				WebElement linkText = driver.findElement(By.xpath(ELEMENTS.getProperty(object)));
				String expected     = CONFIG.getProperty(data);
				APP_LOGS.debug("The expected should be :" + expected);
				if(linkText.getText().equals(expected))
					return Constants.PASS;
				else 
					return Constants.FAIL + "-- LinkText is Wrong --";
				
			}catch(Exception e){
				return Constants.FAIL + "-- LinkText is Wrong --";
			}
	}
		
		
		
	public  String clickButton(String object, String data){
	        
			APP_LOGS.debug("Clicking on Button");
			try{
	        	driver.findElement(By.xpath(ELEMENTS.getProperty(object))).click();
	        	return Constants.PASS;
		    }
		    catch(Exception e){
		        return Constants.FAIL + "-- Unable to click on link" + object;
		    }		
		
	}
		
		
	
	
	
	//****************SPECIFIC KEYWORDS FOR APPLICATION *************************//
		
	
}
